package com.example.api;

import android.util.Log;

import com.example.interfaces.ResponseCallback;
import com.example.viewmodel.CatalogData;
import com.example.viewmodel.CurrentSubCategoryData;
import com.example.viewmodel.HomeData;
import com.example.viewmodel.ResponseData;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

public class ApiService {

    private static final String baseURL = "http://hmapp.net:8888/";

    //首页数据获取
    public static void GetHome(ResponseCallback<ResponseData<HomeData>> callback) {
        OkGo.<String>get(baseURL + "wx/home/index")
//                .tag(this)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        ResponseData<HomeData> result=new Gson().fromJson(response.body(),new TypeToken<ResponseData<HomeData>>(){}.getType());

                        callback.onSuccess(result);
                    }
                });

    }

    //分类页数据获取
    public static void GetCatalog(ResponseCallback<ResponseData<CatalogData>> callback) {
        OkGo.<String>get(baseURL + "wx/catalog/index")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        ResponseData<CatalogData> result=new Gson().fromJson(response.body(),new TypeToken<ResponseData<CatalogData>>(){}.getType());

                        callback.onSuccess(result);
                    }
                });
    }

    //分类页子数据获取
    public static void GetCatalogCurrentCategory(String id, ResponseCallback<ResponseData<CurrentSubCategoryData>> callback) {
        OkGo.<String>get(baseURL + "wx/catalog/current?id="+id)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        ResponseData<CurrentSubCategoryData> result=new Gson().fromJson(response.body(),new TypeToken<ResponseData<CurrentSubCategoryData>>(){}.getType());

                        callback.onSuccess(result);
                    }
                });
    }

}
